<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 25, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "html.HtmlControl" );

/**
 * Class for creating the ajax upload file control
 *
 */
class AjaxUploadControl extends HtmlControl {
	
	var $_waitTime;
	
	var $_linkHandler;
	
	/**
	 * Construtor
	 *
	 * @param string $inputName
	 * @param SiteResource / string $uploadHandler
	 * @param string/array/object $valueOrArrayContain if array normaly it should be: $_GET, $_POST, $_REQUEST
	 * @return AbstractHtmlUploadForm
	 */
	function __construct($inputName, $uploadHandler, $waitTime = 5000) {
		parent::__construct ( $inputName );
		$this->_waitTime = $waitTime;
		if (is_object ( $uploadHandler )) {
			$uploadHandler->setExtend ( "ajax", 1 );
			$uploadHandler = $uploadHandler->getLink ();
		} else {
			$uploadHandler .= "&ajax=1";
		}
		
		$this->_linkHandler = $uploadHandler;
	}
	
	/**
	 * Get the HTML form for ajax uploader
	 *
	 * @return string
	 */
	function createHtmlUpload($size = 50) {
		SysResponse::addJavascriptFile ( SYS_ROOT_URL . "libraries/js/ajaxfileupload.js" );
		
		static $id;
		if (empty ( $id )) {
			$id = 1;
			$script = "
			    arrUploadTimeOut = new Array();
			    arrUploadInputText = new Array();
				function waitingForUpload(element) {
					if (element.type != 'file') {
						return;
					}
					timeOutId = arrUploadTimeOut[element.id];
					if (timeOutId != undefined) {
						clearTimeout(timeOutId);
					}
					var callback = \"ajaxFileUpload('\" + element.id + \"')\"
					timeOutId = setTimeout(callback, {$this->_waitTime});
					arrUploadTimeOut[element.id] = timeOutId;
				}
				
				function changeUpload(elementId) {
					$('#div-'+elementId).html(arrUploadInputText[elementId])
				}
				
				function ajaxFileUpload(elementId)
				{
					//starting setting some animation when the ajax starts and completes
					$('#div-'+elementId)
					.ajaxStart(function(){
						$('#'+elementId).attr('disabled', 'disabled');
					});
					
					$.ajaxFileUpload
					(
						{
							url:'{$this->_linkHandler}',
							secureuri:false,
							fileElementId: elementId,
							dataType: 'json',
							success: function (data, status)
							{
								$('#'+elementId).attr('disabled', '');
													
								if(typeof(data.error) != 'undefined')
								{
									if(data.error != ''){
										alert(data.error);
									}
									else {
										divUpload = $('#div-'+elementId);
										text = divUpload.html();
										arrUploadInputText[elementId] = text;
										text = text.replace('type=\"file\"','type=\"text\"');
										text = text + ' <a href=\"javascript:changeUpload(\''+elementId+'\');\">Change upload</a>';
										divUpload.html(text);
										
										$('#'+elementId).attr('value', data.msg);
									}
								}
							},
							error: function (data, status, e)
							{
								$('#'+elementId).attr('disabled', '');
								alert(e);
							}
						}
					)
					
					return false;
			
				}";
			SysResponse::addJavascriptContent ( $script );
		}
		$id ++;
		$inputId = $this->_inputName . $id;
		$result = "<div class=\"file-upload\" id=\"div-$inputId\">";
		$result .= parent::createInputHtml ( "file", " size=\"$size\" onchange=\"waitingForUpload(this);\"", $inputId );
		$result .= "</div>";
		return $result;
	}
}
?>